package com.goldcn.sys.controller;

import com.goldcn.interceptor.CurrentUser;
import com.goldcn.common.BaseCode;
import com.goldcn.common.CommonUtils;
import com.goldcn.common.ResultData;
import com.goldcn.sys.model.GcnSysRole;
import com.goldcn.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/role")
@Api(value = "/role", description = "角色管理")
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;

	@ApiOperation(value="查询角色列表")
	@RequestMapping(value = "/page/list",method = RequestMethod.POST)
	public String pageList(HttpServletRequest request){
		Map<String,String> param = CommonUtils.getParameterMap(request);
		return sysRoleService.page(param);
	}

	@ApiOperation(value="查询角色")
	@RequestMapping(value = "/id/query", method = RequestMethod.GET)
	public Object getRole(String roleID){
		return  sysRoleService.findRole(roleID);
	}

	@ApiOperation(value="新增角色")
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public Object add(@RequestBody com.goldcn.common.RequestParam params, HttpServletRequest request){
		Map<String,Object> map = (Map<String,Object>)params.getParams();
		CurrentUser user = (CurrentUser) request.getAttribute("user");
		sysRoleService.saveOrUpdateRole((Map) map.get("sysRole"),
				(ArrayList) map.get("pages"),user);
		return new ResultData(BaseCode.SUCCESS_CODE);
	}

	@ApiOperation(value="修改角色")
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public Object update(@RequestBody com.goldcn.common.RequestParam params, HttpServletRequest request){
		return add(params,request);
	}

	@ApiOperation(value="删除角色")
	@RequestMapping(value = "/id/delete",method = RequestMethod.DELETE)
	public Object delete(@RequestBody com.goldcn.common.RequestParam params){
		boolean deleted = sysRoleService.delete(params.getId());
		ResultData resultData = new ResultData();
		resultData.setCode(deleted ? BaseCode.SUCCESS_CODE : BaseCode.ERROR_CODE);
		return resultData;
	}

	@ApiOperation(value="查询角色关联和未关联用户")
	@RequestMapping(value = "/roleUsers",method = RequestMethod.GET)
	public ResultData getRoleUsers(String roleID,HttpServletRequest request){
		CurrentUser user = (CurrentUser) request.getAttribute("user");
		return sysRoleService.getRoleUsersResult(roleID);
	}

	@ApiOperation(value = "获取所有角色")
	@RequestMapping(value = "/jobs",method = RequestMethod.GET)
	public Object jobs(){
		List<GcnSysRole> jobs = sysRoleService.selectAll();
		return new ResultData(jobs);
	}

	@ApiOperation(value="保存角色关联用户")
	@RequestMapping(value = "/addRoleUsers",method = RequestMethod.POST)
	public Object addRoleUsers(@RequestBody com.goldcn.common.RequestParam params, HttpServletRequest request){
		Map<String,Object> map = (Map<String,Object>)params.getParams();
		String roleID = (String) map.get("roleID");
		List<Map> sysUserList = (List<Map>) map.get("list");
		CurrentUser user = (CurrentUser) request.getAttribute("user");
		sysRoleService.saveRoleUsers(roleID,sysUserList ,user);
		return new ResultData(BaseCode.SUCCESS_CODE);
	}

	@ApiOperation(value = "查询用户权限")
	@RequestMapping(value = "/userPermissions",method = RequestMethod.GET)
	public Object getUserPermissions(String userID, HttpServletRequest request){
		return sysRoleService.userPermissions(userID);
	}
}
